import { _decorator, Component, Prefab, Label, find, instantiate, AudioClip, Collider2D, IPhysics2DContact, Contact2DType, CircleCollider2D, PhysicsSystem2D, Director, director } from 'cc';
const { ccclass, property } = _decorator;

import { SSS_Consts } from "../../../Script/Data/SSS_Consts";
import { bdlMgr } from "../../../Script/Manager/BundleManager_S";
import SmallGamePao from "./SmallGamePao";
import { SSS_AudioManager } from 'db://assets/Script/Manager/SSS_AudioManager';

@ccclass('BallPaopao')
export default class BallPaopao extends Component {
    smallGamePao: SmallGamePao = null;
    @property(Prefab)
    tex: Prefab | null = null

    public collider: Collider2D | null = null;

    start() {
        PhysicsSystem2D.instance.enable = true;

        this.smallGamePao = find("Canvas").getComponent(SmallGamePao)

        this.collider = this.getComponent(Collider2D);
        this.collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);

    }
    失败() {
        console.log("失败");

        this.scheduleOnce(() => {
            find("Canvas/Lose").active = true

            let audio_root = `audio/fail`;
            bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, audio_root, AudioClip).then(res => {
                SSS_AudioManager.Instance.playSFX(res, false);
            });

        }, 1);
    }
    成功() {
        console.log("成功");
        // return

        find("Canvas/Win").active = true

        // this.scheduleOnce(() => {
        let r = 4;
        let audio_root = `audio/Win sound/Win sound ${r}`;
        console.log(`audio_root`, audio_root);
        bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, audio_root, AudioClip).then(res => {
            SSS_AudioManager.Instance.playSFX(res, false);
        });
        // }, 1);
    }
    // onBeginContact(contact, selfCollider: PhysicsCollider, otherCollider: PhysicsCollider) {
    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // console.log(`selfCollider`, selfCollider)
        // console.log(`otherCollider`, otherCollider)

        // console.log(`111111111111`)

        if (otherCollider.node.name == "bottom" && selfCollider.node.name == "main") {
            // game.emit('game-ball-drop'),
            // console.log(`2222222222222`)

            this.失败();


            selfCollider.node.name = ""

        }
        if (otherCollider.node.name == "dibu2" && selfCollider.node.name == "main") {
            // console.log(`3333333333333`)

            this.smallGamePao.nowNum++
            // find("Canvas/dibu1/caidai").getComponent(ParticleSystem).enabled=true
            // this.scheduleOnce(()=>{find("Canvas/dibu1/caidai").getComponent(ParticleSystem).enabled=false},2)

            // let tex = instantiate(this.tex)
            // tex.parent = find("Canvas/dibu1")
            // tex.setPosition(0, 0)





            find("Canvas/dibu2/Tips").getComponent(Label).string = this.smallGamePao.nowNum + "/" + this.smallGamePao.levelsmain[this.smallGamePao.level]
            if (this.smallGamePao.nowNum >= this.smallGamePao.levelsmain[this.smallGamePao.level]) {
                this.成功();
                // console.log(`44444444444`)

            }
            else {
                // console.log(`555555555555`)

                let r = 11;
                let audio_root = `audio/Win sound/Win sound ${r}`;
                console.log(`audio_root`, audio_root);
                bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, audio_root, AudioClip).then(res => {
                    SSS_AudioManager.Instance.playSFX(res, false);
                });
            }

            // console.log(`666666666666666`)


            selfCollider.node.name = ""
            // selfCollider.node.active = false

            director.once(Director.EVENT_AFTER_PHYSICS, () => {
                selfCollider.node.destroy();
            }, this)


            // this.scheduleOnce(() => {
            //     selfCollider.node.active = false   // 或者 this.node.destroy();
            // }, 0);
        }
    }
}

